﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;
using FoneBuddy.LinqModel.Data;
using System.Globalization;

public partial class Add_PromotionCode : System.Web.UI.Page
{

    IDataRepository _dataObject = new DataRepository();
   
    #region pageload

    protected void Page_Load(object sender, EventArgs e)
    {
        txtPromotionCode.Text = GeneratePromotionCode();
        if (!IsPostBack)
        {

            BindSubscription();
            if (Request.QueryString["Id"] != null)
            {

            }
            else
            {
                lblHeader.Text = "Add Promotion Code";
            }
        }
    }
    #endregion

    #region save clear and cancel
    protected void Save(object sender, EventArgs e)
    {
        PromotionCode pCode = new PromotionCode();
        try
        {
            pCode = new PromotionCode(_dataObject);
            BuildObject(pCode);
            pCode.CreatedDate = System.DateTime.Now;
            pCode.IpAdded = HttpContext.Current.Request.UserHostAddress;
            pCode.IsDeleted = false;
            if (rbMainType.SelectedValue == "1")
            {
                pCode.DiscountType = Convert.ToInt32(rbType.SelectedValue);
            }
            if (rbMainType.SelectedValue == "2")
                pCode.DiscountType =(Int32)EnumClass.Enums.Discount.Month;
            pCode.UsedCount = 0;
            pCode.Create();
            Response.Redirect("~/PromotionCode-List.aspx");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = "<strong>Error!</strong> Please enter date in dd/MM/yyyy .";
            divMessage.Attributes.Add("class", "alert alert-danger");
            return;
        }
    }
    #endregion

    #region getdata


    private void BuildObject(PromotionCode pCode)
    {
        pCode.Code = txtPromotionCode.Text;
        if (!string.IsNullOrEmpty(txtAllowUser.Text))
            pCode.AllowUser = Convert.ToInt32(txtAllowUser.Text);
        else
            pCode.AllowUser = 0;
        pCode.Discount = Convert.ToDecimal(txtDiscount.Text);
        pCode.StartDate = DateTime.Parse(txtStartDate.Text, CultureInfo.GetCultureInfo("en-gb"));
        pCode.ExpiryDate = DateTime.Parse(txtExpiryDate.Text, CultureInfo.GetCultureInfo("en-gb"));
        pCode.SubscriptionId=Convert.ToInt32(drpSubscription.SelectedValue);
        

    }
    private void BindSubscription()
    {
        Subscription subscription = new Subscription();
        subscription = new Subscription(_dataObject);
        List<Subscription> subscriptions = new List<Subscription>();
        subscriptions = subscription.GetAllSubscription();
        drpSubscription.DataSource = subscriptions;
        drpSubscription.DataBind();
        drpSubscription.Items.RemoveAt(0);
        drpSubscription.Items.Insert(0, new ListItem("Select", "0"));
    }
    private string GeneratePromotionCode()
    {

        PromotionCode pCode = new PromotionCode(_dataObject);
        return pCode.GeneratePromotionCode();
    }

    #endregion
}